package com.wowjoy.dp.sds.controller;

import com.wowjoy.dp.sds.common.RestResult;
import com.wowjoy.dp.sds.domain.Dictionary;
import com.wowjoy.dp.sds.repoistory.DictionaryRepository;
import com.wowjoy.dp.sds.service.DictionaryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "字典项")
@RestController
@RequestMapping(value = "/dictionaries",
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private DictionaryRepository dictionaryRepository;

    @ApiOperation(value = "根据Code和Context获取字典项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典项编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "context", value = "字典项内容", paramType = "query", required = true)
    })
    @GetMapping(value = "/root")
    public RestResult<Dictionary> getDictionary(@RequestParam(name = "code") String code,
                                                @RequestParam(name = "context") String context) {
        return RestResult.success(dictionaryRepository.getByCodeAndContext(code, context));
    }

    @ApiOperation(value = "通过父节点ID获取子节点列表")
    @GetMapping(value = "")
    public RestResult<List<Dictionary>> getByParentId(@ApiParam(value = "所属父节点ID", required = true)
                                                          @RequestParam(name = "parentId") String parentId) {
        return RestResult.success(dictionaryService.getByParentId(parentId));
    }
}
